package com.huanletao.lang;

import com.huanletao.reflect.ReflectUtilsApiTest;
import org.apache.commons.lang3.reflect.MethodUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.commons.lang3.time.DateUtils;


import java.lang.reflect.Method;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * <p>
 *  日期工具类 常用api 示例:
 *
 * </p>
 *
 * @package: com.huanletao.api
 * @description:
 * @className: DateUtilsApiTest
 * @author: @踏雪无痕.
 * @date: Created in 2022/5/30
 * @copyright: Copyright (c) 2022/5/30
 * @version: V1.0
 */
public class DateUtilsApiTest {

    private static boolean isSameDay() {
        final Date date = new Date();
        // 原生写法
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.DATE, 5); // 加5天
        cal.add(Calendar.HOUR_OF_DAY, -5); // 减5小时

        // 使用commons写法
        /**
         * 13:00:00
         * 17:43:00
         * 24:00:00 - (17:43:00-13:00:00)liji立即
         */
        Date newDate1 = DateUtils.addDays(date, 5); // 加5天
        System.out.println("newDate1 = " + newDate1);
        Date newDate2 = DateUtils.addHours(date, -5); // 减5小时
        System.out.println("newDate2 = " + newDate2);
        Date newDate3 = DateUtils.truncate(date, Calendar.DATE); // 过滤时分秒
        boolean isSameDay = DateUtils.isSameDay(newDate1, newDate2); // 判断是否是同一天
        return isSameDay;
    }

    private static void test02() {
        final Date date = new Date();
        final String pattern = "yyyy年MM月dd日";
        // 原生写法
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        String strDate = sdf.format(date);
        System.out.println(strDate);
        // 使用commons写法
        String strDate1 = DateFormatUtils.format(date, pattern);
        System.out.println("strDate1 = " + strDate1);
    }

    private static void test01() throws ParseException {
        final String strDate = "2021-07-04 11:11:11";
        final String pattern = "yyyy-MM-dd HH:mm:ss";
        // 原生写法
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        Date date1 = sdf.parse(strDate);
        // commons写法
        Date date2 = DateUtils.parseDate(strDate, pattern);
    }
}
